package org.four.merchant.service.impl;

import org.four.common.model.CompanyTree;
import org.four.common.model.LayuiPage;
import org.four.common.model.UserBean;
import org.four.common.model.ZhaoPinBean;
import org.four.merchant.mapper.ZslMapper;
import org.four.merchant.service.ZslService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ZslServiceImpl implements ZslService {

    @Autowired
    private ZslMapper zslMapper;

    /**
     * 树
     * @return
     */
    @Override
    public List<CompanyTree> findNavTree() {
        Integer pid = 0;
        List<CompanyTree> navBeans = findTreeNodes(pid);
        return navBeans;
    }

    /**
     * 招聘信息新增
     * @param zhaoPinBean
     * @return
     */
    @Override
    public ZhaoPinBean addZhaoPin(ZhaoPinBean zhaoPinBean) {
        Integer count = zslMapper.queryCount(zhaoPinBean.getComid());
        if (count > 0){
            zslMapper.addZhaoPin(zhaoPinBean);
            zslMapper.updateCount(zhaoPinBean.getComid());
            return zhaoPinBean;
        }else {
            return null;
        }
    }

    /**
     * 查询所有的招聘信息
     * @param limit
     * @param page
     * @param zhaoPinBean
     * @return
     */
    @Override
    public LayuiPage queryZhaoPin(int limit, int page, ZhaoPinBean zhaoPinBean) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("zhaoPinBean",zhaoPinBean);
        int total = zslMapper.queryZhaoPinCount(objectObjectHashMap);
        int st = (page-1)*limit;
        objectObjectHashMap.put("start",st);
        objectObjectHashMap.put("end",limit);
        List<ZhaoPinBean> list = zslMapper.queryZhaoPin(objectObjectHashMap);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setCount(total);
        layuiPage.setMsg("");
        layuiPage.setData(list);
        return layuiPage;
    }

    /**
     * 删除招聘信息
     * @param id
     */
    @Override
    public void delZhaoPin(Integer[] id) {
        zslMapper.delZhaoPin(id);
    }

    /**
     * 查看投递简历的信息
     * @param limit
     * @param page
     * @param id
     * @return
     */
    @Override
    public LayuiPage queryTouDi(int limit, int page, Integer id) {
        int total = zslMapper.queryTouDiCount(id);
        int st = (page-1)*limit;
        List<UserBean> list = zslMapper.queryTouDi(st,limit,id);
        LayuiPage layuiPage = new LayuiPage();
        layuiPage.setCode(0);
        layuiPage.setCount(total);
        layuiPage.setMsg("");
        layuiPage.setData(list);
        return layuiPage;
    }

    @Override
    public void updatestatus(Integer id, Integer status) {
        zslMapper.updatestatus(id,status);
    }

    @Override
    public void dingdan(Integer id) {
        zslMapper.dingdan(id);
    }

    @Override
    public void addComUserCount(Integer id, Integer count) {
        zslMapper.addComUserCount(id,count);
    }

    @Override
    public Integer findCount(Integer id) {
        return zslMapper.findCount(id);
    }

    @Override
    public ZhaoPinBean queryById(Integer id) {
        return zslMapper.queryById(id);
    }

    @Override
    public void updateTime(Integer id) {
        zslMapper.updateTime(id);
    }


    /**
     * 公司发布的岗位 数据
     * @param id
     * @return
     */
    @Override
    public List findComZhaoPin(Integer id) {

        List result = new ArrayList();
        List<Map> comZhaoPin = zslMapper.findComZhaoPin(id);
        String[] date = new String[comZhaoPin.size()];
        for (int i = 0; i < comZhaoPin.size() ; i++) {
            date[i] = comZhaoPin.get(i).get("count").toString();
        }
        String quanzhi = date[0];
        String jianzhi = date[1];
        String shixi = date[2];
        result.add(quanzhi);
        result.add(jianzhi);
        result.add(shixi);
        return result;
    }

    private List<CompanyTree> findTreeNodes(Integer pid) {
        List<CompanyTree> navBeans = zslMapper.findTreeNodes(pid);
        for (CompanyTree NavBean : navBeans) {
            Integer id = NavBean.getId();
            List<CompanyTree> treeNodes = findTreeNodes(id);
            if(treeNodes==null || treeNodes.size()<=0) {
                NavBean.setSpread(true);
            }else {
                NavBean.setChildren(treeNodes);
            }
        }
        return navBeans;
    }
}
